package com.gitee.feizns.utils.file;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * 文件
 * @author feizns
 * @since 2019/5/13
 */
public abstract class FileUtils {

    /**
     * 获取文件后缀名
     * @param filename
     * @return
     */
    public static String getSuffix(String filename) {
        int index = -1;
        return filename != null && (index = filename.lastIndexOf(".")) != -1
                ? filename.substring(index) : "";
    }

    /**
     * 将流写入文件
     * @param inputStream
     * @param filename
     */
    public static void write(InputStream inputStream, String filename) {
        try ( FileOutputStream fos = new FileOutputStream(filename) ) {
            byte[] tmp = new byte[inputStream.available()];
            inputStream.read(tmp);
            fos.write(tmp);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

}
